package cn.rongdao.jrkabao.ui.webview;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.util.ArrayMap;
import android.text.TextUtils;

import com.mifengkong.frtools.http.request.callback.FRBaseObserver;
import com.mifengkong.frtools.ui.activity.FRStartActivity;

import java.util.Map;

import cn.rongdao.jrkabao.bean.identify.MailAndFundVerifyDataBean;
import cn.rongdao.jrkabao.constant.InfoConstant;
import cn.rongdao.jrkabao.model.identify.CreditCardVerificateGetResultDataModel;
import cn.rongdao.jrkabao.ui.IdentificationVerificate.IdentificationWithValidationItem.status.StatusForNonCompliantActivity;
import cn.rongdao.jrkabao.ui.loan.LoanCommitActivity;


/**
 * Created by jiangyongxing on 2017/8/28.
 * 描述：
 */

public class WebViewActivity4Fund extends WebViewActivity {

    public static final int WEBVIEWACTIVITY4FUND_RTURN = 101;

    private String mLoanName;
    private String mPageSource = InfoConstant.PAGE_SOURCE_DEFAULT;
    private Map<String, String> mFundStatusMap = new ArrayMap<>();

    public static void startWithPageSource(Activity context, String url, String productId, String prgeSource, String loanName) {
        Bundle bundle = new Bundle();
        bundle.putString(URL, url);
        bundle.putString(InfoConstant.PAGE_SOURCE, prgeSource);
        bundle.putString(InfoConstant.PRODUCT_ID, productId);
        bundle.putString(InfoConstant.LOAN_NAME, loanName);
        FRStartActivity.startForResult(context, WebViewActivity4Fund.class, bundle, 0);
    }

    public static void start(Context context, String title, String productId) {
        start(context, FRStartActivity.DEFAULT_FLAGS, productId);
    }


    public static void start(Context context, int flags, String productId) {
        Bundle bundle = new Bundle();
        bundle.putString(InfoConstant.PRODUCT_ID, productId);
        FRStartActivity.start(context, WebViewActivity.class, bundle, flags);
    }

    @Override
    protected void initView() {
        super.initView();
        mProductId = getIntent().getStringExtra(InfoConstant.PRODUCT_ID);
        mPageSource = getIntent().getStringExtra(InfoConstant.PAGE_SOURCE);
        mLoanName = getIntent().getStringExtra(InfoConstant.LOAN_NAME);

        mFundStatusMap.put("cate_id", InfoConstant.FUND_CATE_ID);
        mFundStatusMap.put("product_id", mProductId);
        mFundStatusMap.put("edition", "381");
    }

    @Override
    protected void getUserFundData() {
        mFundStatusMap.put("verify_type", "6");//6表示为公积金的verifyType
        CreditCardVerificateGetResultDataModel.getInstance().execute(mFundStatusMap, new FRBaseObserver<MailAndFundVerifyDataBean>(this) {
            @Override
            protected void onBaseNext(final MailAndFundVerifyDataBean data) {
                if (data.getPoll_status() == 1) {
                    if (fromInfoActivity()) return;
                    switch (data.getCode()) {
//                        case InfoConstant.VERIFY_STATUS_REAL_NAME_NOT_MATCH://实名不匹配
//                            startStatusActivity(String.valueOf(InfoConstant.VERIFY_STATUS_REAL_NAME_NOT_MATCH));
//                            break;
                        case InfoConstant.VERIFY_STATUS_MEET_CONDITIONS://满足条件，大额贷
                            LoanCommitActivity.startWithOtherJump(thisActivity(), InfoConstant.FUND_CATE_ID, mProductId, mPageSource, mLoanName);
                            setResult(WEBVIEWACTIVITY4FUND_RTURN);
                            break;
                        case InfoConstant.VERIFY_STATUS_DEFAIL://额度不够  小额贷也没有
                            StatusForNonCompliantActivity.start(getActivity(), InfoConstant.FUND_CATE_ID, String.valueOf(InfoConstant.VERIFY_STATUS_DEFAIL), mPageSource);
                            break;
                        case InfoConstant.VERIFY_STATUS_PETTY_LOAN://小额贷
                            LoanCommitActivity.startWithOtherJump(thisActivity(), InfoConstant.PETTY_LOAN_CATE_ID, mProductId, mPageSource, mLoanName);
                            setResult(WEBVIEWACTIVITY4FUND_RTURN);
                            break;
                    }
                    finish();
                }
            }
        });
    }

    private boolean fromInfoActivity() {
        if (TextUtils.isEmpty(mProductId)) {
            setResult(WEBVIEWACTIVITY4FUND_RTURN);
            finish();
            return true;
        }
        return false;
    }

}
